import { Request, Response } from 'express';
import { getRepository } from 'typeorm';
import { StudyPlan } from '../models/StudyPlan';
import { Category } from '../models/Category';
import { AuthRequest } from '../middlewares/auth';

// 获取学习计划列表
export const getStudyPlans = async (req: AuthRequest, res: Response) => {
  try {
    const studyPlanRepository = getRepository(StudyPlan);
    const studyPlans = await studyPlanRepository.find({
      where: { user: { id: req.user.id } },
      relations: ['category']
    });

    res.json(studyPlans);
  } catch (error) {
    res.status(500).json({ message: '获取学习计划失败', error: error.message });
  }
};

// 获取单个学习计划
export const getStudyPlan = async (req: AuthRequest, res: Response) => {
  try {
    const { id } = req.params;
    const studyPlanRepository = getRepository(StudyPlan);
    const studyPlan = await studyPlanRepository.findOne({
      where: { id: Number(id), user: { id: req.user.id } },
      relations: ['category']
    });

    if (!studyPlan) {
      return res.status(404).json({ message: '学习计划不存在' });
    }

    res.json(studyPlan);
  } catch (error) {
    res.status(500).json({ message: '获取学习计划失败', error: error.message });
  }
};

// 创建学习计划
export const createStudyPlan = async (req: AuthRequest, res: Response) => {
  try {
    const { categoryId, dailyWordCount, dailyReviewCount, studyTimeStart, studyTimeEnd, startDate, endDate } = req.body;
    const studyPlanRepository = getRepository(StudyPlan);
    const categoryRepository = getRepository(Category);

    const category = await categoryRepository.findOne({ where: { id: categoryId } });
    if (!category) {
      return res.status(404).json({ message: '分类不存在' });
    }

    const newStudyPlan = studyPlanRepository.create({
      user: { id: req.user.id },
      category,
      dailyWordCount,
      dailyReviewCount,
      studyTimeStart,
      studyTimeEnd,
      startDate,
      endDate,
      isActive: true
    });

    await studyPlanRepository.save(newStudyPlan);
    res.status(201).json(newStudyPlan);
  } catch (error) {
    res.status(500).json({ message: '创建学习计划失败', error: error.message });
  }
};

// 更新学习计划
export const updateStudyPlan = async (req: AuthRequest, res: Response) => {
  try {
    const { id } = req.params;
    const { dailyWordCount, dailyReviewCount, studyTimeStart, studyTimeEnd, startDate, endDate, isActive } = req.body;
    const studyPlanRepository = getRepository(StudyPlan);

    const studyPlan = await studyPlanRepository.findOne({
      where: { id: Number(id), user: { id: req.user.id } }
    });

    if (!studyPlan) {
      return res.status(404).json({ message: '学习计划不存在' });
    }

    Object.assign(studyPlan, {
      dailyWordCount,
      dailyReviewCount,
      studyTimeStart,
      studyTimeEnd,
      startDate,
      endDate,
      isActive
    });

    await studyPlanRepository.save(studyPlan);
    res.json(studyPlan);
  } catch (error) {
    res.status(500).json({ message: '更新学习计划失败', error: error.message });
  }
};

// 删除学习计划
export const deleteStudyPlan = async (req: AuthRequest, res: Response) => {
  try {
    const { id } = req.params;
    const studyPlanRepository = getRepository(StudyPlan);
    const studyPlan = await studyPlanRepository.findOne({
      where: { id: Number(id), user: { id: req.user.id } }
    });

    if (!studyPlan) {
      return res.status(404).json({ message: '学习计划不存在' });
    }

    await studyPlanRepository.remove(studyPlan);
    res.json({ message: '删除成功' });
  } catch (error) {
    res.status(500).json({ message: '删除学习计划失败', error: error.message });
  }
}; 